package com.meichis.yndms.ui.activity;

import android.graphics.Color;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;

import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;

import com.google.gson.Gson;
import com.meichis.mcsappframework.utils.ScreenUtil;
import com.meichis.yndms.R;
import com.meichis.yndms.databinding.ActivityDailySalesBinding;
import com.meichis.yndms.hybrid.*;
import com.meichis.yndms.model.DailySales;
import com.meichis.yndms.model.ProductSale;
import com.meichis.yndms.ui.common.BaseActivity;
import com.meichis.yndms.vm.DailySalesVM;

import java.util.HashSet;
import java.util.Set;

public class DailySalesActivity extends BaseActivity implements HybridInterface {

    private ActivityDailySalesBinding binding;
    private DailySalesVM vm;


    @Override
    protected void setDataBindingContentView() {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_daily_sales);
        vm = new ViewModelProvider(this, new ViewModelProvider.NewInstanceFactory()).get(DailySalesVM.class);
        binding.setVm(vm);
        binding.setClick(new ClickEvent());
        binding.setLifecycleOwner(this);
    }

    @Override
    protected void init() {
        binding.navigation.txtTitle.setText("日销看板");
        binding.navigation.navBack.setOnClickListener(v -> onBackPressed());
        addObserver();
        vm.GetUserSalesDayKanban();
    }


    private void addObserver() {
        vm.getShowDialog().observe(this, aBoolean -> showLoading(aBoolean));

        vm.getDailySalesList().observe(this, dailySales -> {

            setEchartsData(vm.ProductSalesMoney);
        });
    }

    @Override
    public void execute(int CallType, String ControlID, Object Revalue, String FUNCCallBack) {

    }

    @Override
    public void setActivityResultCallback(CallbackContext callback) {

    }

    public void setEchartsData(int state) {
        Set set = new HashSet();
        for (DailySales dailySale : vm.getDailySalesList().getValue()) {
            for (ProductSale productSale : dailySale.getProductSaleList()) {
                set.add(productSale.getProductType());
                if (state == vm.ProductSalesMoney) {
                    productSale.setNum(productSale.getProductSalesMoney());
                } else {
                    productSale.setNum(productSale.getProductSalesIQuantity());
                }
            }
        }
        ViewGroup.LayoutParams lp;
        lp = binding.llWeb.getLayoutParams();
        lp.width = binding.llWeb.getWidth();
        lp.height = ScreenUtil.dip2px(52) * vm.getDailySalesList().getValue().size() + ScreenUtil.dip2px(10) * set.size();
        binding.llWeb.setLayoutParams(lp);

        //统计图
        MCWebView web = new MCWebView(getActivity());
        MCSWebViewClient webViewClient = new MCSWebViewClient();
        MCSChromeClient webChromeClient = new MCSChromeClient(this);
        web.setWebViewClient(webViewClient);
        web.setWebChromeClient(webChromeClient);
        web.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, 1.0F));
        web.setBackgroundColor(Color.parseColor("#ffffff"));
        //加载本地资源文件
        web.loadUrl("file:///android_asset/DailySales.html");
        binding.llWeb.removeAllViews();
        binding.llWeb.addView(web);
        web.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                web.sendResult("", "JavaCallJs", new Gson().toJson(vm.getDailySalesList().getValue()));
            }
        });
    }


    public class ClickEvent {

        public void onSelectMoney() {
            binding.tvMoney.setTextSize(16);
            binding.tvMoney.setTextColor(getResources().getColor(R.color.white));
            binding.tvMoney.setBackgroundResource(R.drawable.shape_fillet_right_solid_theme);

            binding.tvNum.setTextSize(14);
            binding.tvNum.setTextColor(getResources().getColor(R.color.txt));
            binding.tvNum.setBackground(null);

            binding.tvMsg.setText("业务员昨日销量看板（单位：元）");
            setEchartsData(vm.ProductSalesMoney);

        }

        public void onSelectNum() {
            binding.tvMoney.setTextSize(14);
            binding.tvMoney.setTextColor(getResources().getColor(R.color.txt));
            binding.tvMoney.setBackground(null);

            binding.tvNum.setTextSize(16);
            binding.tvNum.setTextColor(getResources().getColor(R.color.white));
            binding.tvNum.setBackgroundResource(R.drawable.shape_fillet_left_solid_theme);

            binding.tvMsg.setText("业务员昨日销量看板（单位：件）");
            setEchartsData(vm.ProductSalesIQuantity);
        }
    }
}
